//获取员工数据
var getStuData = function(){

	var storage = window.localStorage;
	var stu = JSON.parse(storage.getItem("stu"));
	return stu;
	
}

//展示数据
var displayData = function(){
	var stu = getStuData();
    console.log(stu);
	var tbody = document.getElementById("index-table");
	var rows = tbody.rows.length-1;

	for(var i=rows;i<stu.length;i++){
		var tr = document.createElement("tr");

		var operate = document.createElement("td");
		operate.innerHTML = 
		"<td><input type='button' name='editBtn' value='修改' onclick='editData(this)'><input type='button' id='delBtn' value='删除' onclick='delData(this)'></td>";
		var order = document.createElement("td");
		order.innerHTML = i+1;
		order.className="order";
		var cusName = document.createElement("td");
		cusName.innerHTML = stu[i].cusName;
		cusName.className = "cusName";
		var cusPhone = document.createElement("td");
		cusPhone.innerHTML = stu[i].cusPhone;
		cusPhone.className = "cusPhone";
		var cusDegree = document.createElement("td");
		cusDegree.innerHTML = stu[i].cusDegree;
		cusDegree.className = "cusDegree";
		var cusAge = document.createElement("td");
		cusAge.innerHTML = stu[i].cusAge;
		cusAge.className = "cusAge";
		var cusPay = document.createElement("td");
		cusPay.innerHTML = stu[i].cusPay;
		cusPay.className = "cusPay";
		// console.log(order.nodeType());
		tr.appendChild(order);
		tr.appendChild(cusName);
		tr.appendChild(cusPhone);
		tr.appendChild(cusDegree);
		tr.appendChild(cusAge);
		tr.appendChild(cusPay);
		tr.appendChild(operate);




		tbody.appendChild(tr);
	}
}

//保存数据
var saveData = function(stu){
	var storage = window.localStorage;
	storage.setItem("stu",JSON.stringify(stu));

	displayData();
}
var stu=[
		{cusName:"示例:孙二",cusPhone:"示例:13988888888",cusDegree:"示例:文盲",cusAge:"示例:0",cusPay:"示例:1"},
	]

	var storage = window.localStorage;
	storage.setItem("stu",JSON.stringify(stu));


//页面初始化
window.onload = function(){
	displayData();
	document.getElementById("cusName").focus();//获取焦点
}

//增加员工信息
var addData = function(){

	var data = getStuData();
	
	var cusName = document.getElementById("cusName").value;
	var cusPhone = document.getElementById("cusPhone").value;
	var cusDegree = document.getElementById("cusDegree").value;
	var cusAge = document.getElementById("cusAge").value;
	var cusPay = document.getElementById("cusPay").value;



	var t = check(cusName,cusPhone,cusDegree,cusAge,cusPay);
		
	if(t == 0){
		return ;
	}

	var stu = {cusName:cusName,cusPhone:cusPhone,cusDegree:cusDegree,cusAge:cusAge,cusPay:cusPay};
	
	data.push(stu);
	saveData(data);
	clearInput();

}

//清空输入框
var clearInput = function(){
	document.getElementById("cusName").value = "";
	document.getElementById("cusPhone").value = "";
	document.getElementById("cusDegree").value = "";
	document.getElementById("cusAge").value = "";
	document.getElementById("cusPay").value = "";


}

// 删除员工数据
var delData = function(e){
	if(confirm("确认删除信息吗？")){
		var tr = e.closest("tr")
		tr.remove();
		
		var stu = getStuData();
		stu.splice(tr.rowIndex-1,1);
		saveData(stu);
	}
}


//编辑数据
var editData = function(e){
	var tr = e.closest("tr");
	document.getElementById("cusName").value = tr.getElementsByClassName("cusName")[0].innerHTML;
	document.getElementById("cusPhone").value = tr.getElementsByClassName("cusPhone")[0].innerHTML;
	document.getElementById("cusDegree").value = tr.getElementsByClassName("cusDegree")[0].innerHTML;
	document.getElementById("cusAge").value = tr.getElementsByClassName("cusAge")[0].innerHTML;
	document.getElementById("cusPay").value = tr.getElementsByClassName("cusPay")[0].innerHTML;

	document.getElementById("saveBtn").removeAttribute("disabled");
	document.getElementById("saveBtn").name = tr.rowIndex;
	
}

//保存编辑的数据
var saveEdit = function(e){

	if(confirm("确认修改信息？")){

		var cusName = document.getElementById("cusName").value;
		var cusPhone = document.getElementById("cusPhone").value;
		var cusDegree = document.getElementById("cusDegree").value;
		var cusAge = document.getElementById("cusAge").value;
		var cusPay = document.getElementById("cusPay").value;



		var t = check(cusName,cusPhone,cusDegree,cusAge,cusPay);

		if(t == 0){
			return ;
		}

		var row = e.getAttribute("name");
		
		var stu = getStuData();

		stu[row-1].cusName = cusName;
		stu[row-1].cusPhone = cusPhone;
		stu[row-1].cusDegree = cusDegree;
		stu[row-1].cusAge = cusAge;
		stu[row-1].cusPay = cusPay;



		document.getElementsByClassName("cusName")[row].innerHTML = cusName;
		document.getElementsByClassName("cusPhone")[row].innerHTML = cusPhone;
		document.getElementsByClassName("cusDegree")[row].innerHTML = cusDegree;
		document.getElementsByClassName("cusAge")[row].innerHTML = cusAge;
		document.getElementsByClassName("cusPay")[row].innerHTML = cusPay;


		saveData(stu);
		clearInput();
		document.getElementById("saveBtn").setAttribute("disabled","disabled");
	}

}

var check = function(cusName,cusPhone,cusDegree,cusAge,cusPay){
	var msg;
	var code;
	if(cusName.length == 0){
		msg = "姓名不能为空!";
		code = 1;
	}else if(cusPhone.length == 0){
		msg = "联系方式不能为空!";
		code = 2;
	}else if(cusDegree.length == 0){
		msg = "学历不能为空!";
		code = 3;
	}else if(cusAge.length == 0){
		msg = "年龄不能为空!";
		code = 4;
	}else if(cusPay.length == 0){
		msg = "期望薪资不能为空!";
		code = 5;
	}else{
		code = 0;//成功;
		msg = "";
	}

	if(code != 0){
		if(code == 1){
			document.getElementById("msg1").innerHTML = msg;
			document.getElementById("cusName").focus();//获取焦点

		}else if(code == 2){
			document.getElementById("msg2").innerHTML = msg;
			document.getElementById("cusPhone").focus();//获取焦点
		}else if(code == 3){
			document.getElementById("msg3").innerHTML = msg;
			document.getElementById("cusDegree").focus();//获取焦点
		}else if(code == 4){
			document.getElementById("msg4").innerHTML = msg;
			document.getElementById("cusAge").focus();//获取焦点
		}else{
			document.getElementById("msg5").innerHTML = msg;
			document.getElementById("cusPay").focus();//获取焦点
		}

		return 0;
	}else{

		document.getElementById("msg1").innerHTML = msg;
		document.getElementById("msg2").innerHTML = msg;
		document.getElementById("msg3").innerHTML = msg;
		document.getElementById("msg4").innerHTML = msg;
		document.getElementById("msg5").innerHTML = msg;



		return 1;
	}
}
